;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.InOutCase4purTask", [])
        .controller("InOutCase4purTaskCtrl", ["$scope", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "$timeout", "$filter", function ($scope, ybWidget, ybHttp, MESG_TYPE, UserContextService, $timeout, $filter) {
            // 高级搜索的展示与隐藏
            $scope.toolbarConfig = {};

            // 切换高级搜索展示与隐藏
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                    return;
                }
                $timeout(function () {
                    $scope.toolbarConfig["show" + toolbar] = true;
                });
            };

            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/saletask/exportReport")
            };


            // 列表查询条件
            $scope.condition = {
                isDealer: !$scope.isZHJCH
            };
            // 表格信息
            $scope.tbConf = {
                firstTableCol: [
                    {
                        name: '代理商',
                        dataField: 'dealerShortName',
                        width: 120,
                        rowSpan: 2
                    },
                    {
                        name: '代理商全称',
                        dataField: 'dealerName',
                        width: 260,
                        rowSpan: 2
                    },
                    {
                        name: '服务代表',
                        dataField: 'serviceRep',
                        width: 120,
                        rowSpan: 2
                    },
                    {
                        name: '代理商采购完成情况',
                        width: 480,
                        colSpan: 4
                    },
                    {
                        name: '代理商终端出库情况',
                        width: 240,
                        colSpan: 2
                    },
                    {
                        name: '库存情况(万)',
                        width: 120,
                    },
                ],
                secondTableCol: [
                    {
                        name: '任务',
                        dataField: 'taskNum',
                        width: 120,
                    },
                    {
                        name: '已完成',
                        dataField: 'completed',
                        width: 120,
                    },
                    {
                        name: '完成率',
                        dataField: 'completedRate',
                        width: 120,
                    },
                    {
                        name: '新增订单',
                        dataField: 'newNum',
                        width: 120,
                    },
                    {
                        name: '总出库',
                        dataField: 'outNum',
                        width: 120,
                    },
                    {
                        name: '现金销售',
                        dataField: 'moneyNum',
                        width: 120,
                    },
                    {
                        name: '库存金额',
                        dataField: 'inventroyAmount',
                        width: 120,
                    },
                ]
            };

            // 分页信息
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.dataList = [];
            $scope.search = function (curPage) {
                if (curPage) $scope.pageInfo.curPage = curPage;
                getDataList();
            };

            function getDataList() {
                var url = "/saletask/list4Report";
                var param = {
                    filters: angular.copy($scope.condition),
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                };
                param.filters.monthSearch = !!$scope.condition.startDate || !!$scope.condition.endDate;
                
                ybHttp.Post('device', url, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    const dataT = resp.value.list || [];
                    const groups = {};
                    dataT.forEach(g => {
                        const groupId = g['area'];
                        groups[groupId] = groups[groupId] || []
                        groups[groupId].push(g)
                    })

                    const dataRes = []
                    Object.keys(groups).forEach(k => {
                        dataRes.push({
                            areaData: groups[k],
                            areaName: k
                        });
                    })

                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = dataRes;
                });
            }

            $scope.reset = function () {
                $scope.condition = {
                    isDealer: !$scope.isZHJCH
                };
            };


            // 初始化页面信息
            (function () {
                getDataList();
            })()
        }])
        ;
})(window, window.angular);